// Do file that puts together the data to be used for the study
//"Big Brother Sees You"

clear
set more off

*Save data on turnout for later use
use "D:\SCB_ConPol\Stata\VD\Valdelt_2010.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen RostRI = .
replace RostRI = 0 if r==1
replace RostRI = 1 if r==2
replace RostRI = 1 if inrange(r, 4, 6)
gen RostLT = .
replace RostLT = 0 if l==1
replace RostLT = 1 if l==2
replace RostLT = 1 if inrange(l, 4, 6)
gen RostKO = .
replace RostKO = 0 if k==1
replace RostKO = 1 if k==2
replace RostKO = 1 if inrange(k, 4, 6)
keep LopNr RostRI RostLT RostKO
tempfile Vd1992
save `Vd1992'

use "D:\SCB_ConPol\Stata\VD\Valdelt_1994rkl.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen RostRI = .
replace RostRI = 0 if r==1
replace RostRI = 1 if r==2
replace RostRI = 1 if inrange(r, 4, 6)
gen RostLT = .
replace RostLT = 0 if l==1
replace RostLT = 1 if l==2
replace RostLT = 1 if inrange(l, 4, 6)
gen RostKO = .
replace RostKO = 0 if k==1
replace RostKO = 1 if k==2
replace RostKO = 1 if inrange(k, 4, 6)
keep LopNr RostRI RostLT RostKO
tempfile Vd1976
save `Vd1976'

use "D:\SCB_ConPol\Stata\VD\Valdelt_82.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen RostRI = .
replace RostRI = 0 if r==1
replace RostRI = 1 if r==2
replace RostRI = 1 if inrange(r, 4, 6)
gen RostLT = .
replace RostLT = 0 if l==1
replace RostLT = 1 if l==2
replace RostLT = 1 if inrange(l, 4, 6)
gen RostKO = .
replace RostKO = 0 if k==1
replace RostKO = 1 if k==2
replace RostKO = 1 if inrange(k, 4, 6)
keep LopNr RostRI RostLT RostKO
tempfile Vd1964
save `Vd1964'


*Save data on nominated and elected for later use
foreach year in 1964 1967 1970 1976 {
use "D:\SCB_ConPol\Stata\NomVald\Nominerade_KO_82_85_88_94.dta", clear
local i = `year'+18
keep if valar==`i'
local i = 0
rename nrinom ListPlaceringKO
rename partikod PartiKodKO
rename partinamn PartiNamnKO
destring ListPlaceringKO, replace force
bysort LopNr: egen occ = rank(ListPlaceringKO), track
keep if occ==1
bysort LopNr: gen nocc = _n
keep if nocc==1
keep LopNr ListPlaceringKO PartiKodKO PartiNamnKO
gen NomKO = 1
tempfile NomKO`year'
save `NomKO`year''
}

foreach level in LT RI {
foreach year in 1964 1967 1970 {
use "D:\SCB_ConPol\Stata\NomVald\Nominerade_`level'_82_85_88.dta", clear
local i = `year'+18
keep if valar==`i'
local i = 0
bysort LopNr: gen occ = _n
keep if occ==1
keep LopNr
gen Nom`level' = 1
tempfile Nom`level'`year'
save `Nom`level'`year''
}
}

use "D:\SCB_ConPol\Stata\NomVald\Nom_1991_KO.dta", clear
bysort LopNr: gen occ = _n
keep if occ==1
rename partikod PartiKodKO
rename partinamn PartiNamnKO
rename Ripartikod RIPartiKodKO
keep LopNr PartiKodKO PartiNamnKO RIPartiKodKO
gen NomKO = 1
tempfile NomKO1973
save `NomKO1973'

foreach year in 1998 2002 2006 2010 2014 {
use "D:\SCB_ConPol\Stata\NomVald\Nom_`year'_KO.dta", clear
rename nrinom ListPlaceringKO
rename partikod PartiKodKO
rename partinamn PartiNamnKO
rename Ripartikod RIPartiKodKO
destring ListPlaceringKO, replace force
bysort LopNr: egen occ = rank(ListPlaceringKO), track
keep if occ==1
bysort LopNr ListPlaceringKO: gen nocc = _n
keep if nocc==1
keep LopNr ListPlaceringKO PartiKodKO PartiNamnKO RIPartiKodKO
gen NomKO = 1
local i = `year'-18
tempfile NomKO`i'
save `NomKO`i''
}

foreach level in LT RI {
foreach year in 1991 1994 1998 2002 2006 2010 2014 {
use "D:\SCB_ConPol\Stata\NomVald\Nom_`year'_`level'.dta", clear
bysort LopNr: gen occ = _n
keep if occ==1
keep LopNr
gen Nom`level' = 1
local i = `year'-18
tempfile Nom`level'`i'
save `Nom`level'`i''
}
}

foreach level in KO LT RI {
foreach year in 1964 1967 1970 {
use "D:\SCB_ConPol\Stata\NomVald\valda_`level'_82_85_88.dta", clear
local i = `year'+18
keep if valar==`i'
bysort LopNr: gen occ = _n
keep if occ==1
keep LopNr
gen Vald`level' = 1
tempfile Vald`level'`year'
save `Vald`level'`year''
}
}

foreach level in KO LT RI {
foreach year in 1991 1994 1998 2002 2006 2010 2014 {
use "D:\SCB_ConPol\Stata\NomVald\Valda_`year'_`level'.dta", clear
bysort LopNr: gen occ = _n
keep if occ==1
keep LopNr
gen Vald`level' = 1
local i = `year'-18
tempfile Vald`level'`i'
save `Vald`level'`i''
}
}

clear

*Save Fodelseuppg for later use
use "D:\SCB_ConPol\Stata\RTB\Fodelseuppg.dta", replace 
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
tempfile Fodelseuppg
save `Fodelseuppg'

*Save Fodelseuppg for mothers for later use
use "D:\SCB_ConPol\Stata\RTB\Fodelseuppg.dta", replace 
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr FodArMan
replace FodArMan = floor(FodArMan/100)
rename FodAr UppMorFodAr
tempfile FodelseuppgMor
save `FodelseuppgMor'

clear

*Save DodDat for later use
use "D:\SCB_ConPol\Stata\RTB\DodDat.dta", replace
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
tempfile DodDat
save `DodDat'

clear

*Save education and occupation from LISA in election years for later use
use "D:\SCB_ConPol\Stata\LISA\LISA_1998.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr Sun2000Inr Sun2000niva
tempfile LISA_1998
save `LISA_1998'

foreach year in 2002 2006 2010 {
use "D:\SCB_ConPol\Stata\LISA\LISA_`year'.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr Sun2000Inr Sun2000niva SSYK4 SSYKAr
destring SSYK4, replace force
destring SSYKAr, replace force
tempfile LISA_`year'
save `LISA_`year''
}

use "D:\SCB_ConPol\Stata\LISA\LISA_2014.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr Sun2000Inr Sun2000niva ssyk4_2012 SSYKAr
rename ssyk4_2012 SSYK4
destring SSYK4, replace force
destring SSYKAr, replace force
tempfile LISA_2014
save `LISA_2014'


*Save education from FoB and LISA for later use
use "D:\SCB_ConPol\Stata\FoB\FoB_70.dta", replace
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr UtbNiva
gen UtbAr1970 = 7 if UtbNiva==1
replace UtbAr1970 = 9 if UtbNiva==2
replace UtbAr1970 = 11 if UtbNiva==3
replace UtbAr1970 = 12 if UtbNiva==4
replace UtbAr1970 = 14 if UtbNiva==5
replace UtbAr1970 = 15.5 if UtbNiva==6
replace UtbAr1970 = 19 if UtbNiva==7
drop UtbNiva
tempfile FoB_1970
save `FoB_1970'

foreach year in 1990 2000 {
use "D:\SCB_ConPol\Stata\LISA\LISA_`year'.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr Sun2000niva
gen UtbAr = 7 if Sun2000niva<200
replace UtbAr = 10 if inrange(Sun2000niva, 200, 299)
replace UtbAr = 9. if Sun2000niva==204
replace UtbAr = 10 if inrange(Sun2000niva, 310, 319)
replace UtbAr = 11 if inrange(Sun2000niva, 320, 329)
replace UtbAr = 12 if inrange(Sun2000niva, 330, 339)
replace UtbAr = 13 if inrange(Sun2000niva, 410, 419)
replace UtbAr = 14 if inrange(Sun2000niva, 520, 529)
replace UtbAr = 15 if inrange(Sun2000niva, 530, 539)
replace UtbAr = 16 if inrange(Sun2000niva, 540, 549)
replace UtbAr = 17 if inrange(Sun2000niva, 550, 559)
replace UtbAr = 18 if inrange(Sun2000niva, 600, 629)
replace UtbAr = 19 if inrange(Sun2000niva, 640, 649)
drop Sun2000niva
rename UtbAr UtbAr`year'
tempfile LISA_`year'
save `LISA_`year''
}


clear

*Save municipality of residence for each election year for later use
foreach year in 1982 1985 1988 1991 1994 1998 2002 2006 2010 2014 {
use "D:\SCB_ConPol\Stata\RTB\RTB_`year'.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr Kommun
local i = `year'-18
tempfile RTB_`i'
save `RTB_`i''
}

clear

*Save conscription data for later use
use "D:\SCB_ConPol\Stata\Monstring\insark.dta", clear
keep LopNr PPRF_BEFL PPRF_PF PPRF_PGRP
destring PPRF_BEFL PPRF_PF PPRF_PGRP, replace force
bysort LopNr: egen BEFL = max(PPRF_BEFL)
bysort LopNr: egen PF = max(PPRF_PF)
bysort LopNr: egen PGRP = max(PPRF_PGRP)
bysort LopNr: gen occ = _n
keep if occ==1
drop PPRF_BEFL PPRF_PF PPRF_PGRP occ
rename BEFL PPRF_BEFL
rename PF PPRF_PF
rename PGRP PPRF_PGRP
tempfile insark
save `insark'

clear

use "D:\SCB_ConPol\Stata\Monstring\da856.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
tempfile da856
save `da856'

clear

*Save MGR files for later use
use "D:\SCB_ConPol\Stata\FlerGen\BioForaldrar.dta", clear
rename lopnrmor LopNrMor
rename lopnrfar LopNrFar
rename morantbarn MorAntBarn
rename farantbarn FarAntBarn
rename morordnr MorOrdNr
rename farordnr FarOrdNr
tempfile BioForaldrar
save `BioForaldrar'

use "D:\SCB_ConPol\Stata\FlerGen\AdForaldrar.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
tempfile AdForaldrar
save `AdForaldrar'


set more off
clear
*Create sample and merge data
/*For some reason there are more obs with info on sibling order according to birth mother
than there are obs with serial number for the birth number (same pattern for the fathers)
*/


foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
*Get sample from MGR
use `BioForaldrar', clear
drop if LopNrMor==.
bysort LopNrMor: egen mOrdNr = min(MorOrdNr)
drop if mOrdNr == 0
drop if MorOrdNr == 0
drop mOrdNr

*Merge to birth month and sex
merge 1:1 LopNr using `Fodelseuppg', keepusing(LopNr FodArMan UtlSvBakG Kon)
drop if _merge==2
drop _merge
gen ValAr = `year'+18
order LopNr ValAr

*Merge to death date
merge 1:1 LopNr using `DodDat'
drop if _merge==2
drop _merge AterPnr SenPnr

*Add indicator for being adoptee and adoptee family
merge 1:m LopNr using `AdForaldrar', keepusing(LopNrAdMor LopNrAdFar)
drop if _merge==2
gen Adopterad = 1 if LopNrAdMor!=.
replace Adopterad = 1 if LopNrAdFar!=.
gen occ = 1 if LopNrMor==LopNrAdMor
replace occ = 1 if LopNrFar==LopNrAdFar
bysort LopNrMor: egen nocc = count(occ)
drop if nocc>0 & nocc!=.
drop occ nocc _merge
bysort LopNrAdMor: egen AdopteradFam = count(Adopterad)
replace AdopteradFam = 1 if AdopteradFam!=0 & AdopteradFam!=.

*Drop twin families
gen occ = 1
bysort LopNrMor FodArMan: gen nocc = sum(occ)
replace nocc = . if LopNrMor==.
replace nocc = . if nocc==1
bysort LopNrMor: egen TvillingFam = count(nocc)
replace TvillingFam = 1 if TvillingFam>1 & TvillingFam!=.
drop if TvillingFam==1
drop occ nocc TvillingFam


*Add maternal age
rename LopNr aLopNr
rename LopNrMor LopNr
rename FodArMan aFodArMan
merge m:1 LopNr using `Fodelseuppg', keepusing(FodArMan)
drop if _merge==2
gen MorFodAr = floor(FodArMan/100)
drop FodArMan _merge
rename LopNr LopNrMor
rename aLopNr LopNr
rename aFodArMan FodArMan


*Add indicator for early deaths within family/mother
gen occ = floor(DodDatum/100)
gen nocc = occ-FodArMan
*gen TidigDod = 1 if inrange(nocc, 0, 6)
*replace TidigDod = 1 if inrange(nocc, 89, 94)
gen TidigDod = 1 if inrange(nocc, 0, 12)
replace TidigDod = 1 if inrange(nocc, 89, 100)
replace TidigDod = . if occ==.
bysort LopNrMor: egen TidigDodFam = total(TidigDod)
replace TidigDodFam = 1 if inrange(TidigDodFam, 2, 20)
drop occ nocc
  
*Generate birth and rearing order and fam size by birth and rearing family (by mother)
bysort LopNrMor: egen FodMorOrdNr = rank(FodArMan), track
gen LopNrUppMor = LopNrMor
replace LopNrUppMor = LopNrAdMor if Adopterad==1
replace LopNrUppMor = . if TidigDod==1  
bysort LopNrUppMor: egen UppMorOrdNr = rank(FodArMan), track
drop if LopNrUppMor==.
bysort LopNrMor: egen FodMorAntBarn = count(LopNr)
bysort LopNrUppMor: egen UppMorAntBarn = count(LopNr)

tempfile `year'
save ``year''
}


clear


*Add on data
*Turnout
foreach year in 1964 1976 1992 {
use ``year'', clear
merge 1:1 LopNr using `Vd`year''
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Nominated and elected
foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
foreach elect in Nom Vald {
foreach level in KO LT RI {
merge 1:1 LopNr using ``elect'`level'`year''
drop if _merge==2
drop _merge
replace `elect'`level' = 0 if `elect'`level'!=1
}
}
tempfile `year'
save ``year'', replace
}

clear
*Municipality of residence during election years
foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge 1:1 LopNr using `RTB_`year''
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Education from LISA
foreach year in 1980 1984 1988 1992 1996 {
use ``year'', clear
local i = `year'+18
merge 1:1 LopNr using `LISA_`i'', keepusing(Sun2000niva Sun2000Inr)
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Occupation LISA - use occupation from 2002 for the election in 1998
use `1980', clear
merge 1:1 LopNr using `LISA_2002', keepusing(SSYK4 SSYKAr)
drop if _merge==2
drop _merge
tempfile 1980
save `1980', replace

foreach year in 1984 1988 1992 1996 {
use ``year'', clear
local i = `year'+18
merge 1:1 LopNr using `LISA_`i'', keepusing(SSYK4 SSYKAr)
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Conscription data
foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge 1:1 LopNr using `insark'
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge 1:1 LopNr using `da856', keepusing(Befl Psyf Gkap)
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Rearing mother birth year
foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
rename LopNr aLopNr
rename LopNrUppMor LopNr
merge m:1 LopNr using `FodelseuppgMor'
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

*Rearing mother education
foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge m:1 LopNr using `FoB_1970'
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge m:1 LopNr using `LISA_1990'
drop if _merge==2
drop _merge
tempfile `year'
save ``year'', replace
}

foreach year in 1964 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
use ``year'', clear
merge m:1 LopNr using `LISA_2000'
drop if _merge==2
drop _merge
rename LopNr LopNrUppMor
rename aLopNr LopNr
tempfile `year'
save ``year'', replace
}

*Append the datasets and erase temporary files
use `1964', clear
foreach year in 1967 1970 1973 1976 1980 1984 1988 1992 1996 {
append using ``year''
}


*Recode and label variables
gen FodAr = floor(FodArMan/100)

gen UtbAr = 7 if Sun2000niva<200
replace UtbAr = 10 if inrange(Sun2000niva, 200, 299)
replace UtbAr = 9. if Sun2000niva==204
replace UtbAr = 10 if inrange(Sun2000niva, 310, 319)
replace UtbAr = 11 if inrange(Sun2000niva, 320, 329)
replace UtbAr = 12 if inrange(Sun2000niva, 330, 339)
replace UtbAr = 13 if inrange(Sun2000niva, 410, 419)
replace UtbAr = 14 if inrange(Sun2000niva, 520, 529)
replace UtbAr = 15 if inrange(Sun2000niva, 530, 539)
replace UtbAr = 16 if inrange(Sun2000niva, 540, 549)
replace UtbAr = 17 if inrange(Sun2000niva, 550, 559)
replace UtbAr = 18 if inrange(Sun2000niva, 600, 629)
replace UtbAr = 19 if inrange(Sun2000niva, 640, 649)

gen UtbInr = substr(Sun2000Inr, 1, 1)
destring UtbInr, replace
replace Sun2000niva = floor(Sun2000niva/100)
replace SSYK4 = floor(SSYK4/100)


egen BEFL = rowmax(PPRF_BEFL Befl)
replace BEFL = . if BEFL==0

egen PF = rowmax(PPRF_PF Psyf)
replace PF = . if PF==0

egen IQ = rowmax(PPRF_PGRP Gkap)
replace IQ = . if IQ==0

drop PPRF_BEFL PPRF_PF PPRF_PGRP Befl Psyf Gkap


gen ValAlder = ValAr - FodAr

replace ListPlaceringKO = 0 if ListPlaceringKO!=1
replace ListPlaceringKO = 0 if ValdKO==0
replace ListPlaceringKO = . if ValAr==1991
replace ListPlaceringKO = . if NomKO==.

foreach var of varlist NomKO NomLT NomRI ValdKO ValdLT ValdRI ListPlaceringKO {
replace `var' = `var'*100
}

gen Nom = NomKO
replace Nom = NomLT if NomLT==100 & Nom==0
replace Nom = NomRI if NomRI==100 & Nom==0

gen Vald = ValdKO
replace Vald = ValdLT if ValdLT==100 & Vald==0
replace Vald = ValdRI if ValdRI==100 & Vald==0

gen UppMorUtbAr = UtbAr1970
replace UppMorUtbAr = UtbAr1990 if UppMorUtbAr==.
replace UppMorUtbAr = UtbAr1990 if UtbAr1990>UppMorUtbAr
replace UppMorUtbAr = UtbAr2000 if UppMorUtbAr==.
replace UppMorUtbAr = UtbAr2000 if UtbAr1970>UppMorUtbAr
drop UtbAr1970 UtbAr1990 UtbAr2000

*Rearing mother education standardized by birth cohort
egen mean_utb = mean(UppMorUtbAr), by(UppMorFodAr)
egen sd_utb = sd(UppMorUtbAr), by(UppMorFodAr)
gen stdUppMorUtbAr = (UppMorUtbAr - mean_utb)/sd_utb
drop mean_utb sd_utb


label var ValAr "Election year"
label var Adopterad "Indicator for being adopted"
label var NomKO "Nominated to the municipal assembly"
label var NomLT "Nominated to the county assembly"
label var NomRI "Nominated to the national parliament"
label var ListPlaceringKO "List placement, municipal election"
label var ValdKO "Elected to the municipal assembly"
label var ValdLT "Elected to the county assembly"
label var ValdRI "Elected to the national parliament"
label var FodAr "Birth year"
label var FodMorOrdNr "Birth order"
label var UppMorOrdNr "Rearing order"
label var UtbAr "Years of schooling in election year"
label var BEFL "Officer aptitude rating"
label var PF "Non-cognitive rating"
label var IQ "Cognitive test score, total"
label var MorFodAr "Birth mother's birth year"
label var ValAlder "Age at election"
label var Sun2000niva "SUN 1-digit categories for educational attainment"
label var Sun2000Inr "SUN 3-digit categories for educational content"
label var UtbInr "SUN 1-digit categories for educational content"
label var SSYK4 "2-digit occupational codes"
label var RostRI "Turnout in national election"
label var PartiKodKO "Code for party in municipal election"
label var PartiNamnKO "Party name in municipal election"
label var RIPartiKodKO "National parliament code for party in municipal election"
label var UppMorUtbAr "Rearing mother's years of education"
label var UppMorFodAr "Rearing mother's birth year"
label var stdUppMorUtbAr "Within birth cohort standardized rearing mother's years of education"


save "D:\Data\ProjData\BigBrother/ReplicationData.dta", replace

